package com.kingtown.inventory.adapter;


import android.app.Application;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kingtown.inventory.MainApplication;
import com.kingtown.inventory.R;
import com.kingtown.inventory.domain.dto.AssetDto;
import com.kingtown.inventory.domain.dto.TaskDetailItemDto;

import java.util.List;


public class AssetAdapter extends RecyclerView.Adapter<AssetAdapter.ViewHolder> implements View.OnClickListener {
    private List<AssetDto> items;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View assetView;
        TextView assetName, inventoryStatus, assetCode, assetAddress, extendLabel1, extendValue1;
        ImageView assetImage;

        public ViewHolder(@NonNull View view) {
            super(view);
            assetView = view;
            assetName = (TextView) view.findViewById(R.id.asset_name);
            inventoryStatus = (TextView) view.findViewById(R.id.inventory_status);
            assetCode = (TextView) view.findViewById(R.id.asset_code);
            assetAddress = (TextView) view.findViewById(R.id.asset_address);

        }
    }

    public AssetAdapter(List<AssetDto> items, MainApplication mApplication) {
        Log.i("AssetAdapter","在AssetAdapter中实时获取application中的变量："+mApplication.showMode);
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.asset_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AssetDto item = items.get(position);
        Log.i("onBindViewHolder", item.toString());

//        if (StrUtil.isNotBlank(item.getExtend())) {
//            JSONArray array = JSONUtil.parseArray(item.getExtend());
//            array.forEach(s -> {
//                JSONObject obj = (JSONObject) s;
////                Integer index = obj.getInt("index");
//                String extendLabel = obj.getStr("label");
//                String extendValue = obj.getStr("value");
//                holder.extendLabel1.setVisibility(View.VISIBLE);
//                holder.extendValue1.setVisibility(View.VISIBLE);
//                holder.extendLabel1.setText(extendLabel);
//                holder.extendValue1.setText(extendValue);
//
//            });
//        }
        holder.assetName.setText(item.getAssetName());
        holder.assetCode.setText(item.getAssetCode());
        holder.assetAddress.setText(item.getAddress());
        holder.inventoryStatus.setText(convertInventoryStatus(item.getInventoryStatus()));

        if (item.getInventoryStatus().equals(1)) {
            holder.inventoryStatus.setBackgroundColor(ContextCompat.getColor(holder.assetView.getContext(), R.color.primary));
        } else {
            holder.inventoryStatus.setBackgroundColor(ContextCompat.getColor(holder.assetView.getContext(), R.color.error));
        }
        holder.assetView.setTag(item);

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    private OnItemClickListener onItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onClick(v, v.getTag());
        }
    }

    private String convertInventoryStatus(Integer status) {
        return switch (status) {
            case 0 -> "未盘";
            case 1 -> "已盘";
            case 2 -> "多盘";
            case 3 -> "已扫描";
            default -> "未盘";
        };
    }
}